package com.eatos.milktea.web;

import com.eatos.milktea.entity.Banner;
import com.eatos.milktea.entity.vo.MyResult;
import com.eatos.milktea.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController//返回一个controller
@CrossOrigin//跨域
@RequestMapping("/banner")//全局的映射路径
public class BannerController {
    @Autowired//拿service，调用service
    private BannerService bannerService;

    @RequestMapping("/getlist")//映射路径，小路径
    public MyResult getlist(){
        MyResult myResult = new MyResult();//将中数据库中返回的结果，封装到MyResult 中
        try{
            //调用BannerService中的getBannerList()
            //getBannerList()类型是一个集合，要用list接收
            List<Banner> bannerList = bannerService.getBannerList();
            if(bannerList!=null && bannerList.size()>0){
                myResult.setStatusCode(200);
                myResult.setMessage("success");
                myResult.setMydata(bannerList);
            }
            else{
                myResult.setStatusCode(404);
                myResult.setMessage("没有查询到广告列表");
                myResult.setMydata(null);
            }
        }catch (Exception ex){
            System.out.println(ex);
            myResult.setStatusCode(500);
            myResult.setMessage("查询广告列表出错了");
        }
        return myResult;

    }

}
